home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Complementary Applications 2004 February / SGI IRIX 6.5 Complementary Applications 2004 February.iso / dist / cde.idb / usr / dt / share / examples / dtdnd / text.c.z / text.c
Encoding:
C/C++ Source or Header  |  2003-11-18  |  9.8 KB  |  378 lines

  1. /*
  2.  * text.c
  3.  *
  4.  * Copyright 2000, Silicon Graphics, Inc.
  5.  * ALL RIGHTS RESERVED
  6.  * 
  7.  * UNPUBLISHED -- Rights reserved under the copyright laws of the United
  8.  * States.   Use of a copyright notice is precautionary only and does not
  9.  * imply publication or disclosure.
  10.  *
  11.  * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
  12.  * Use, duplication or disclosure by the Government is subject to restrictions
  13.  * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
  14.  * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
  15.  * in similar or successor clauses in the FAR, or the DOD or NASA FAR
  16.  * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
  17.  * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
  18.  *
  19.  * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
  20.  * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
  21.  * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
  22.  * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
  23.  * GRAPHICS, INC.
  24.  */
  25. /* $XConsortium: text.c /main/cde1_maint/1 1995/07/17 16:43:40 drk $ */
  26. /*****************************************************************************
  27.  *****************************************************************************
  28.  **
  29.  **   File:         text.c
  30.  **
  31.  **   Description:  Text transfer functions for the CDE Drag & Drop Demo.
  32.  **
  33.  **  (c) Copyright 1993, 1994 Hewlett-Packard Company
  34.  **  (c) Copyright 1993, 1994 International Business Machines Corp.
  35.  **  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  36.  **  (c) Copyright 1993, 1994 Unix System Labs, Inc., a subsidiary of
  37.  **      Novell, Inc.
  38.  **
  39.  ****************************************************************************
  40.  ************************************<+>*************************************/
  41.  
  42. #include <X11/Intrinsic.h>
  43.  
  44. #include <Xm/Xm.h>
  45. #include <Xm/Label.h>
  46. #include <Xm/List.h>
  47. #include <Xm/RowColumn.h>
  48. #include <Xm/Text.h>
  49.  
  50. #include <Dt/Dt.h>
  51. #include <Dt/Dnd.h>
  52.  
  53. #include "demo.h"
  54. #include "text.h"
  55.  
  56.  /*************************************************************************
  57.  *
  58.  *       Data Structures & Private Declarations For Text Transfers
  59.  *
  60.  **************************************************************************/
  61.  
  62. /*
  63.  * Data for text list of fruit
  64.  */
  65.  
  66. char *todaysFruit[] = {
  67.         "Oranges",
  68.         "Peaches",
  69.         "Lemons",
  70.         "Watermelons",
  71.         "Apples",
  72.         "Bananas",
  73.         "Plums",
  74.         "Limes",
  75.         "Cantaloupes",
  76.         "Nectarines",
  77.         "Papayas",
  78.         "Mangos",
  79.         NULL
  80. };
  81.  
  82.  /*************************************************************************
  83.  *
  84.  *      Text Drag & Drop
  85.  *
  86.  *************************************************************************/
  87.  
  88. /*
  89.  * textConvertCallback
  90.  *
  91.  * Sets the text object's text to the text in the fruit list based on where
  92.  * the pointer was when the drag started.
  93.  */
  94. void
  95. textConvertCallback(
  96.         Widget          dragContext,
  97.         XtPointer       clientData,
  98.     XtPointer       callData)
  99. {
  100.         DtDndConvertCallbackStruct *convertInfo =
  101.                                         (DtDndConvertCallbackStruct *) callData;
  102.         Widget          fruitList = (Widget) clientData;
  103.         int             selectedPos;
  104.         XmString       *items;
  105.         Cardinal        itemCount;
  106.  
  107.     if (convertInfo == NULL) {
  108.         return;
  109.     }
  110.  
  111.     /*
  112.      * Verify protocol and callback reason
  113.      */
  114.  
  115.         if (convertInfo->dragData->protocol != DtDND_TEXT_TRANSFER ||
  116.         (convertInfo->reason != DtCR_DND_CONVERT_DATA &&
  117.          convertInfo->reason != DtCR_DND_CONVERT_DELETE) ||
  118.         fruitList == NULL) {
  119.                 return;
  120.     }
  121.  
  122.         switch (convertInfo->reason) {
  123.         case DtCR_DND_CONVERT_DATA:
  124.  
  125.         /*
  126.           * Provide the text from the fruit list
  127.          */
  128.  
  129.                 XtVaGetValues(fruitList,
  130.                         XmNuserData,    &selectedPos,
  131.                         XmNitems,       &items,
  132.                         XmNitemCount,   &itemCount,
  133.                         NULL);
  134.  
  135.                 if (itemCount > 0 && selectedPos < itemCount) {
  136.                         convertInfo->dragData->data.strings[0] =
  137.                                         items[selectedPos-1];
  138.                 } else {
  139.                         convertInfo->status = DtDND_FAILURE;
  140.                 }
  141.                 break;
  142.         DtCR_DND_CONVERT_DELETE:
  143.  
  144.         /*
  145.          * Delete the text from the fruit list. If the fruit list
  146.          * were set up to be dynamic, deletion from the list would
  147.          * occur here.
  148.          */
  149.  
  150.                 printf("Delete fruit item #%d\n",
  151.                         convertInfo->dragData->data.strings[0]);
  152.                 break;
  153.         }
  154. }
  155.  
  156. /*
  157.  * textDragFinishCallback
  158.  *
  159.  * Normally would free any memory allocated by textConvertCallback
  160.  * but none was allocated so this is just a placeholder.
  161.  */
  162. void
  163. textDragFinishCallback(
  164.         Widget          widget,
  165.         XtPointer       clientData,
  166.         XtPointer       callData)
  167. {
  168. }
  169.  
  170. /*
  171.  * textTransferCallback
  172.  *
  173.  * Handles transfer of files or text to the text edit. Files are transfered
  174.  * by placing their name in the field, text by inserting the text into the
  175.  * field.
  176.  */
  177. void
  178. textTransferCallback(
  179.         Widget          widget,
  180.         XtPointer       clientData,
  181.         XtPointer       callData)
  182. {
  183.         DtDndTransferCallbackStruct *transferInfo =
  184.                 (DtDndTransferCallbackStruct *) callData;
  185.         String      text;
  186.  
  187.     /*
  188.      * Verify callback reason
  189.      */
  190.  
  191.     if (transferInfo == NULL || 
  192.         transferInfo->reason != DtCR_DND_TRANSFER_DATA) {
  193.         return;
  194.     }
  195.  
  196.         switch (transferInfo->dropData->protocol) {
  197.  
  198.         case DtDND_FILENAME_TRANSFER:
  199.  
  200.         /*
  201.          * Copy the file name into the text field
  202.          */
  203.  
  204.                 XtVaSetValues(widget,
  205.                         XmNvalue, transferInfo->dropData->data.files[0],
  206.                         NULL);
  207.  
  208.                 break;
  209.  
  210.         case DtDND_TEXT_TRANSFER:
  211.  
  212.         /*
  213.          * Copy the fruit name into the text field
  214.          */
  215.  
  216.                 XmStringGetLtoR(transferInfo->dropData->data.strings[0],
  217.                         XmFONTLIST_DEFAULT_TAG, &text);
  218.  
  219.                 XtVaSetValues(widget, XmNvalue, text, NULL);
  220.  
  221.                 break;
  222.         }
  223. }
  224.  
  225. /*
  226.  * textDragSetup
  227.  *
  228.  * Prepares the fruit list to source drags of text with button 1.
  229.  */
  230. void
  231. textDragSetup(
  232.     Widget        fruitList)
  233. {
  234.         XtAddEventHandler(fruitList, Button1MotionMask, False,
  235.                 (XtEventHandler)demoDragMotionHandler,
  236.                 (XtPointer)DtDND_TEXT_TRANSFER);
  237. }
  238.  
  239. /*
  240.  * textDropSetup
  241.  *
  242.  * Registers text field to accept drops of files.
  243.  */
  244. void
  245. textDropSetup(
  246.         Widget          textField)
  247. {
  248.         static XtCallbackRec transferCBRec[] = { {textTransferCallback, NULL},
  249.                                                  {NULL, NULL} };
  250.  
  251.         DtDndDropRegister(textField, DtDND_FILENAME_TRANSFER,
  252.                 XmDROP_COPY, transferCBRec, NULL, 0);
  253. }
  254.  
  255. /*
  256.  * textDragStart
  257.  *
  258.  * Initiates a drag of a text item from the fruit list provided the pointer
  259.  * is over an item in the list.
  260.  */
  261. void
  262. textDragStart(
  263.         Widget          widget,
  264.         XEvent         *event)
  265. {
  266.         int             itemCount,
  267.             selectedPos;
  268.  
  269.         static XtCallbackRec convertCBRec[] = { {textConvertCallback, NULL},
  270.                                                 {NULL, NULL} };
  271.         static XtCallbackRec dragFinishCBRec[] =
  272.                           { {demoDragFinishCallback, NULL},
  273.                             {textDragFinishCallback, NULL},
  274.                                                 {NULL, NULL} };
  275.  
  276.     /*
  277.      * Determine which item to drag from the text list
  278.      */
  279.  
  280.         XtVaGetValues(widget, XmNitemCount, &itemCount, NULL);
  281.  
  282.         selectedPos = XmListYToPos(widget, event->xmotion.y);
  283.  
  284.         if (selectedPos == 0 || selectedPos > itemCount) {
  285.                 return;
  286.         }
  287.  
  288.         XtVaSetValues(widget, XmNuserData, selectedPos, NULL);
  289.  
  290.         convertCBRec[0].closure = (XtPointer)widget;
  291.  
  292.     /*
  293.      * Start the drag
  294.      */
  295.  
  296.         if (DtDndDragStart(widget, event, DtDND_TEXT_TRANSFER, 1,
  297.             XmDROP_COPY, convertCBRec, dragFinishCBRec, NULL, 0)
  298.             == NULL) {
  299.  
  300.                 printf("DragStart returned NULL.\n");
  301.         }
  302. }
  303.  
  304.  /*************************************************************************
  305.  *
  306.  *      Text Creation & Initialization
  307.  *
  308.  *************************************************************************/
  309.  
  310. /*
  311.  * textCreateDragSource
  312.  *
  313.  * Creates a scrolling list filled with fruit names.
  314.  */
  315. Widget
  316. textCreateDragSource(
  317.         Widget          parent)
  318. {
  319.         Widget          fruitList;
  320.         XmString       *fruits;
  321.         Arg             args[2];
  322.         int             ii, fruitCount;
  323.  
  324.         for (ii = 0; todaysFruit[ii] != NULL; ii++)
  325.         ;
  326.         fruitCount = ii;
  327.  
  328.         fruits = (XmString *) XtMalloc(sizeof(XmString) * fruitCount);
  329.  
  330.         for (ii = 0; ii < fruitCount; ii++) {
  331.                 fruits[ii] = XmStringCreate(todaysFruit[ii],
  332.                                             XmFONTLIST_DEFAULT_TAG);
  333.         }
  334.  
  335.         ii = 0;
  336.         XtSetArg(args[ii], XmNitems,      fruits);     ii++;
  337.         XtSetArg(args[ii], XmNitemCount,  fruitCount); ii++;
  338.  
  339.         fruitList = XmCreateScrolledList(parent, "fruitList", args, ii);
  340.         XtManageChild(fruitList);
  341.  
  342.         for (ii = 0; ii < fruitCount; ii++) {
  343.                 XmStringFree(fruits[ii]);
  344.         }
  345.     XtFree((char *)fruits);
  346.  
  347.         return fruitList;
  348. }
  349.  
  350. /*
  351.  * textCreateDropSite
  352.  *
  353.  * Creates a text field with a label.
  354.  */
  355. Widget
  356. textCreateDropSite(
  357.     Widget        parent)
  358. {
  359.     Widget        textRowColumn,
  360.             textLabel,
  361.             textField;
  362.  
  363.         textRowColumn = XtVaCreateManagedWidget("textRowColumn",
  364.                 xmRowColumnWidgetClass, parent,
  365.                 NULL);
  366.  
  367.         textLabel = XtVaCreateManagedWidget("textLabel",
  368.                 xmLabelWidgetClass, textRowColumn,
  369.                 NULL);
  370.  
  371.         textField = XtVaCreateManagedWidget("textField",
  372.                 xmTextWidgetClass, textRowColumn,
  373.                 NULL);
  374.  
  375.     return textField;
  376. }
  377.  
  378.